<template>
    <div
        style="background-color: rgb(241, 241, 241);padding:5px;padding-right: 7px;min-height: 100vh; box-sizing: border-box;">
        <van-nav-bar title="我的" left-text="返回" left-arrow fixed @click="$router.push(`/home`)">
        </van-nav-bar>
        <div style="height: 50px;"></div>
        <div v-if="getToken()" style="color: #333">
            <!-- 个人信息 -->
            <div style="background-color: #fff;padding: 10px;border-radius: 10px;display: flex;"
                @click="$router.push('/user-info')">
                <img :src="curUser?.avatar" alt="" style="width: 88px;height: 88px;border-radius:10px;margin-left: 5px;">
                <div style="margin-left: 15px;">
                    <p style="font-size: 24px;margin-top: 5px;">{{ curUser?.name }}</p>
                    <p style="font-size: 14px;margin-top: 10px;">{{ curUser?.sex ? '男' : '女' }}</p>
                    <p style="font-size: 14px;margin-top: 10px;">{{ curUser?.sign }}</p>
                </div>
            </div>

            <div style="margin-top: 10px;border-radius: 10px;background-color: #fff;overflow: hidden;">
                <van-cell icon="logistics" title="我的收货地址" is-link to="/address" @click="removeGoodsId"/>
                <van-cell icon="orders-o" title="我的订单" is-link to="/my-order" />
                <van-cell icon="records" title="我的文章" is-link to="/my-article" />
                <van-cell icon="star-o" title="我的收藏" is-link to="/my-collection" />
                <van-cell icon="good-job-o" title="我的点赞" is-link to="/my-praise" />
                
            </div>
            <van-button type="warning" color="#1BAEAE" @click="exit"
                style="width: 94vw;margin-top: 5vh;margin-left: 1.2vw;">退出登录</van-button>
        </div>

        <!-- 判断是否处于登录状态 -->
        <van-empty description="未登录" v-if="!getToken()">
            <van-button style="background-color: #1BAEAE;color: #fff; width: 180px;" @click="$router.push('/login')"> 登
                录</van-button>
        </van-empty>
    </div>
</template>

<script setup>
import { logout, getUserData } from "@/api/index.js"
import { useRouter } from 'vue-router'
import { removeToken, getToken } from "../../utils/token";
import { lastLoginUser,getUserInfo} from '../../utils/localStorage'
import useStore from '../../store/useStore'
import useCartStore from "../../store/useCartStore";

let router = useRouter()
let token = getToken('mltoken')
let cartStore = useCartStore()
let curUser = lastLoginUser()

if (token) {
    getUserData(curUser)
} else {
    cartStore.clearCartInfo()
}

//移除商品id
function removeGoodsId(){
    localStorage.removeItem('curGoodsId')
}
//退出登录
function exit() {
    logout(token).then(res => {
        //console.log(res)
        if (res.code == 1) {
            removeToken()
            cartStore.clearCartInfo()
            router.replace('/login')
        }
    })
}

</script>